<?php

// 本类由系统自动生成，仅供测试用途
class IndexAction extends BaseAction {
 	function verify(){
		  
		import('ORG.Util.Image');
		
        Image::buildImageVerify();
		
      }

      function t(){
          $str = I("get.key");
          $s= "垃圾,混蛋,狗日";
          $arr = explode(",",$s);
          $info = str_replace($arr,"MBL",$str);
          $count = substr_count($info,"MBL");
          $this->ajaxReturn(array("count"=>$count));
	  }

	private $asklist =array(

	   array("ask"=>"请问⑩加上⑥等于多少","q"=>"16"),

	    array("ask"=>"问⑥乘于①等于多少","q"=>"6"),

	     array("ask"=>"请问⑥加上⑦等于多少","q"=>"13"),

	      array("ask"=>"请问①加上⑩等于多少","q"=>"11"),

	       array("ask"=>"请问⑩加上⑧等于多少","q"=>"18"),

	        array("ask"=>"请问⑧乘于④等于多少","q"=>"32"),

	         array("ask"=>"请问③加上⑤等于多少","q"=>"8"),

	          array("ask"=>"请问④乘于④等于多少","q"=>"16"),

	           array("ask"=>"请问②乘于⑥等于多少","q"=>"12"),

	            array("ask"=>"请问④加上⑤等于多少","q"=>"9"),

	);

	/*
	 * 首页
	 * */
	function index(){

           if(isMobile()){
               header("location:allcity.php");
           	 exit();
		   }

        import('ORG.Util.Page');// 导入分页类
        $count      =  M('Zhaopin')->where('status=1')->count();// 查询满足要求的总记录数
        $Page       = new Page($count,150);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $temp = M('Zhaopin')->where('status=1 and top!=1 and top!=3')->limit($Page->firstRow.','.$Page->listRows)->order('top DESC,createtime desc')->select();

        foreach ($temp as $v){

            $cate =M('Category')->where("id=".$v['cid'])->find();

            $v['city'] = M('City')->where("id=".$v['cityid'])->getField("pinyin");

            $v['catename'] = $cate['name'];

            $v['catepath'] = $cate['path'];

            $info[]=$v;

        }
        $this->assign('zhaopin',$info);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出




        //import('ORG.Util.Page');// 导入分页类
	   // $Page       = new Page(500,50);// 实例化分页类 传入总记录数和每页显示的记录数
	  //  $show       = $Page->show();
        if(S('infom')){
        	$infom = S('infom');
        }else{

        	$tempm = M('Zhaopin')->where('status=1 and top!=1 and top!=3')->limit(0, 100)->order('top DESC,createtime desc')->select();

	        foreach ($tempm as $u) {

	            $cate = M('Category')->where("id=" . $u['cid'])->find();

	            $u['city'] = M('City')->where("id=" . $u['cityid'])->getField("pinyin");

	            $u['catename'] = $cate['name'];

	            $u['catepath'] = $cate['path'];

	            $infom[] = $u;

	        }
	        S("infom",$infom,60); 
        }

        //广告位

        $adinfo =M('Ad')->where(array('type'=>'homeads','endtime'=>array('gt',time())))->order('id ASC')->select();

        $this->assign("adinfo",$adinfo);
		  //print_r($info);
		/*友情链接*/
		$linklist = M('link')->where(array('cityid'=>array('in','0,371')))->order('id ASC')->select();

//推广
		$spread = M('Zhaopin')->where("spread_sort <> 0 and spread_sort_time > '".date('Y-m-d H:i:s')."' ")->order('spread_sort +0 asc')->select();
		foreach ($spread as $u) {

			$cate = M('Category')->where("id=" . $u['cid'])->find();

			$u['city'] = M('City')->where("id=" . $u['cityid'])->getField("pinyin");

			$u['catename'] = $cate['name'];

			$u['catepath'] = $cate['path'];

			$spreadArr[] = $u;

		}
		$this->assign('spread', $spreadArr);
		$this->assign('info',$info);
		$this->assign('infom',$infom);
		$this->assign('linklist',$linklist);
		$this->display();
	         

	    
	 

	}




	function home(){
		
		import('ORG.Util.Page');// 导入分页类
        
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        //$temp = M('Zhaopin')->where('status=1 and top!=1 and top!=3')->limit($Page->firstRow.','.$Page->listRows)->order('top DESC,createtime desc')->select();
             
	   if (!empty($this->cityid)){
		   
		if ($this->cityid == 370){
			$count      =  M('Zhaopin')->where('status=1')->count();// 查询满足要求的总记录数
            $Page       = new Page($count,150);// 实例化分页类 传入总记录数和每页显示的记录数
            $show       = $Page->show();// 分页显示输出  
		   $temp = M('Zhaopin')->where("status=1 AND top=0")->limit($Page->firstRow.','.$Page->listRows)->order('top=2 DESC,createtime desc')->select();
		}else{
		   $count      =  M('Zhaopin')->where('status=1 AND cityid='.$this->cityid)->count();// 查询满足要求的总记录数
           $Page       = new Page($count,150);// 实例化分页类 传入总记录数和每页显示的记录数
           $show       = $Page->show();// 分页显示输出  
		   $temp = M('Zhaopin')->where("status=1 AND top=0  AND cityid=".$this->cityid)->limit($Page->firstRow.','.$Page->listRows)->order('top=2 DESC,createtime desc')->select();
	     
		 }
	   }else{
		 $count      =  M('Zhaopin')->where('status=1 AND areaid='.$this->areaid)->count();// 查询满足要求的总记录数
         $Page       = new Page($count,150);// 实例化分页类 传入总记录数和每页显示的记录数
         $show       = $Page->show();// 分页显示输出    
		 $temp = M('Zhaopin')->where("status=1 AND top=0  AND areaid=".$this->areaid)->limit($Page->firstRow.','.$Page->listRows)->order('top=2 DESC,createtime desc')->select();  
	   }
	   
	   //十万火急
		$param['top'] = array('in','4');
		$param['toptime'] = array('gt',time());
		$huojitemp1 = M('Zhaopin')->where($param)->order("createtime desc")->select();
        $param['top'] = array('in','3');
        $param['toptime'] = array('gt',time());
        $param['cityid'] = $this->cityid;
        $huojitemp2 = M('Zhaopin')->where($param)->order("createtime desc")->select();
        if(empty($huojitemp1)){
        	$huoji1 = array();
		}else{
        	foreach ($huojitemp1 as $v){
                $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
                $huoji1[]=$v;
			}
		}
		if(empty($huojitemp2)){
        	$huoji2 = array();
		}else{
            foreach ($huojitemp2 as $v){
                $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
                $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
                $huoji2[]=$v;
            }
		}
        $huoji = array_merge($huoji1,$huoji2);




	   //城市置顶
	   $topinfo1 = M('Zhaopin')->where("status=1 AND  cityid=".$this->cityid." AND top=1 AND toptime>".mktime())->order("createtime desc")->select();
	   //全局置顶
	   $topinfo2 = M('Zhaopin')->where("status=1 AND top=2 AND toptime>".mktime())->order("createtime desc")->select();

	   $cityadlist = M('ad')->where('cityid='.$this->cityid)->select();   // 得到以城市分类的广告

	   $cityname = M('city')->where('id='.$this->cityid)->getField('city');
	   $citypinyin = M('city')->where('id='.$this->cityid)->getField('pinyin');
	   if(!isset($cityname)){
		   $cityname = M('city')->where('id='.$areainfo['id'])->getField('name');
	   }
	   if(!empty($temp)){
	       $arrr = "";
	       if(!empty($topinfo2)){
	           $arrr = $topinfo2;
	       }
	       if(!empty($topinfo1)){
	           $arrr = array_merge($arrr,$topinfo1);
	       }

	       if(!empty($arrr)){
	           $temp = array_merge($arrr,$temp);
	       }



	   }else{
	       $arrr = "";
	       if(!empty($topinfo2)){
	           $arrr = $topinfo2;
	       }
	       if(!empty($topinfo1)){
	           $arrr = array_merge($arrr,$topinfo1);
	       }
	       $temp = $arrr;
	   }
	   foreach ($temp as $v){
			 $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
	         $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
	         $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
             $v['createtime']= date("m-d",$v['createtime']);
	         $list[]=$v;
	        
	   }

	   $this->flash =M('Flash')->select();

	   $temp1 =  M('Zhaopin')->where('status=1')->limit(100)->order("id DESC")->select();
	   
	   foreach ($temp1 as $v){
		   $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
		   $v['cityname']=M('City')->where('id='.$v['cityid'])->getField("pinyin");
	       $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
           $v['createtime']= date("Y-m-d",$v['createtime']);
	       $qita[]=$v;
	   }
		
	   //手机端任务列表分页显示
	   import('ORG.Util.Page');// 导入分页类
	   if (!empty($this->cityid)){
	   if ($this->cityid == 370){
			$count_m      =  M('Zhaopin')->where('status=1')->count();// 查询满足要求的总记录数
            $Page_m       = new Page($count_m,150);// 实例化分页类 传入总记录数和每页显示的记录数
            $show_m       = $Page_m->show();// 分页显示输出  
		   $temp_m = M('Zhaopin')->where("status=1 AND top=0")->limit($Page_m->firstRow.','.$Page_m->listRows)->order('top=2 DESC,createtime desc')->select();
		}else{
		   $count_m      =  M('Zhaopin')->where('status=1 AND cityid='.$this->cityid)->count();// 查询满足要求的总记录数
           $Page_m      = new Page($count_m,150);// 实例化分页类 传入总记录数和每页显示的记录数
           $show_m       = $Page_m->show();// 分页显示输出  
		   $temp_m = M('Zhaopin')->where("status=1 AND top=0  AND cityid=".$this->cityid)->limit($Page_m->firstRow.','.$Page_m->listRows)->order('top=2 DESC,createtime desc')->select();
		}
	   }else{
		 $count_m      =  M('Zhaopin')->where('status=1 AND areaid='.$this->areaid)->count();// 查询满足要求的总记录数
         $Page_m       = new Page($count_m,150);// 实例化分页类 传入总记录数和每页显示的记录数
         $show_m       = $Page_m->show();// 分页显示输出    
		 $temp_m = M('Zhaopin')->where("status=1 AND top=0  AND areaid=".$this->areaid)->limit($Page_m->firstRow.','.$Page_m->listRows)->order('top=2 DESC,createtime desc')->select();  
	   }
	   if(!empty($temp_m)){
	       $arrr = "";
	       if(!empty($topinfo2)){
	           $arrr = $topinfo2;
	       }
	       if(!empty($topinfo1)){
	           $arrr = array_merge($arrr,$topinfo1);
	       }
	       
	       if(!empty($arrr)){
	           $temp_m = array_merge($arrr,$temp_m);
	       }
	      
	       
	   
	   }else{
	       $arrr = "";
	       if(!empty($topinfo2)){
	           $arrr = $topinfo2;
	       }
	       if(!empty($topinfo1)){
	           $arrr = array_merge($arrr,$topinfo1);
	       }
	       $temp_m = $arrr;
	   }
	   foreach ($temp_m as $v){
			 $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
	         $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
	         $v['city']= M('City')->where("id=".$v['cityid'])->getField('pinyin');
             $v['createtime']= date("Y-m-d",$v['createtime']);
	         $list_m[]=$v;
	        
	   }


	   $Page1       = new Page(500,100);// 实例化分页类 传入总记录数和每页显示的记录数
	   $show1       = $Page1->show();
	   $temp_m1 =  M('Zhaopin')->where('status=1')->limit($Page1->firstRow.','.$Page1->listRows)->order("id DESC")->select();
	   
	   foreach ($temp1 as $v){
		   $v['cate'] =M('Category')->where("id=".$v['cid'])->getField("name");
		   $v['cityname']=M('City')->where('id='.$v['cityid'])->getField("pinyin");
	       $v['road'] =M('Category')->where("id=".$v['cid'])->getField("path");
           $v['createtime']= date("Y-m-d",$v['createtime']);
	       $qita_m[]=$v;
	   }
	   
	   
	   $this->company =M('Company')->where("cityid=".$this->cityid." AND category > 0")->order("id DESC")->limit(8)->select();
	   
	  
	   $catelink = M('link')->where('status=1 AND cityid='.$this->cityid)->select();   //城市分类友情链接
	   
	   
	   
	   $tempcate = M('Category')->where('status=1')->select();	   //分类
	   foreach ($tempcate as $k=>$v){
	   	$str= M('Category')->where('status=1 and id='.$v['id'])->getField('name');	   	
	   	$x=count($tempcate)-1;	   	
	    if($k === $x){
	    	 $catelistx.= $str;
	    }else{
	    	$catelistx.=$str.'、';	    	
	    }
	   
	   }
	   $catelist=cutStr($catelistx,27);   //截取分类字段
	   
	   $this->cityad = M('Ad')->where(array('type'=>'cityads','endtime'=>array('gt',time()),'cityid'=>array('in','0,'.$this->cityid.'')))->select();

	   if (!empty($this->cityid)){
         $this->diqu = M('area')->where(array('cityid'=>$this->cityid))->select();
	   }else{
		 $this->diqu = M('area')->where(array('cityid'=>$this->areainfo['cityid']))->select();
	   }
	   if(!isset($cityname)){
		   $cityname = M('area')->where("pinyin='".$this->city."'")->getField('name');
		   $areacityname = M('city')->where("id='".$this->areainfo['cityid']."'")->getField('city');
		   $areacitypinyin = M('city')->where("id='".$this->areainfo['cityid']."'")->getField('pinyin');
		   $this->assign("areacityname",$areacityname);
		   $this->assign("areacitypinyin",$areacitypinyin);
	   }
		//推广
		$spread = M('Zhaopin')->where("spread_sort <> 0 and spread_sort_time > '".date('Y-m-d H:i:s')."' ")->order('spread_sort +0 asc')->select();
		foreach ($spread as $u) {

			$cate = M('Category')->where("id=" . $u['cid'])->find();

			$u['city'] = M('City')->where("id=" . $u['cityid'])->getField("pinyin");

			$u['catename'] = $cate['name'];

			$u['catepath'] = $cate['path'];

			$spreadArr[] = $u;

		}
		$this->assign('spread', $spreadArr);

	   $this->assign("cityadlist",$cityadlist);
	   $this->assign("catelist",$catelist);
	   $this->assign("list",$list);
	   $this->assign("catelink",$catelink);
	   //$this->assign("cityname",$cityname);
	   $this->assign("cityname",$cityname);
	   $this->assign("citypinyin",$citypinyin);
	   $this->assign("qita",$qita);
	   $this->assign("list_m",$list_m);
	   $this->assign("qita_m",$qita_m);
	   $this->assign('page',$show);
	   $this->assign('page1',$show1);
       $this->assign("huoji",$huoji);


	   $this->display();

	}

	function publish(){

 		$catelist=M('Category')->where('status=1')->select();		
		$this->assign('catelist',$catelist);

		$this->display();

	}

	/*

	 * 发布信息

	 * Enter description here ...

	 */

	function addinfo(){

	    if($this->uinfo ==0){

	    
		 header("Location: ".U('Public/login'));  exit();
	   //   $this->error("您还没有登录无法发布信息",U('Public/login'));

	    }
	    //判断垃圾关键字
        if(checkStr($_POST['titles'])){
            $this->error("标题有非法关键字");
	    	exit();
		}
        if(checkStr($_POST['neirong'])){
            $this->error("内容有非法关键字");
            exit();
        }


        //判断是否认证
	/**	$rz = M('Renzheng')->where(array('uid'=>$this->uinfo['uid']))->getField("status");
	    if($rz != 1){
	    	$this->error("认证用户才能发布");
	    	exit();
		} */

		if(isset($_POST['tijiao'])){

			if($_SESSION['verify'] != md5($_POST['verify'])){

 	  	  	  	   header("Location: ".U('User/addinfo'));

 	  	  	   }

 	  	  	   //本用户已经发布的信息数量

 	  	  	   $year = date("Y");

				$month = date("m");

				$day = date("d");

				$dayBegin = mktime(0,0,0,$month,$day,$year);//当天开始时间戳

				$dayEnd = mktime(23,59,59,$month,$day,$year);//当天结束时间戳

 	  	  	

 	  	  	    $count = M('Zhaopin')->where("uid=".$this->uinfo['uid']." AND is_batch = 0 AND createtime>".$dayBegin." AND createtime <".$dayEnd)->count();
 	  	  	 

 	  	  	   //判断是否是VIP

		
 	  	  	   if($this->uinfo['vip']==1){

 	  	  	     //可以发布10条

 	  	  	      if($count >= 100){

 	  	  	       $this->error("VIP用户每天只能发布100条");

 	  	  	      	exit();

 	  	  	      }

 	  	  	   }else{

 	  	  	     //可以发布1条

 	  	  	      if($count >= 1){

 	  	  	    $this->error("普通用户每天只能发布1条");

 	  	  	      	exit();

 	  	  	      }

			   }
			  
		

 	  	  	 $data['cityid']=$_POST['cityid'];
             $data['areaid'] = I('post.areaid');
 	  	  	 $data['title'] = $_POST['titles'];

 	  	  	 $data['content'] =$_POST['neirong'];

 	  	  	 $data['keywords']=$_POST['keywords'];

 	  	  	 $data['shopname']=$_POST['shopname'];
			 
			 //$data['cover']=$this->upfile();
			 if ($_POST['photo_base64'] !==""){
			 $data['cover']=$this->saveBase64Image($_POST['photo_base64']);
             }
 	  	  	 $data['contact']=$_POST['contact'];

 	  	  	 $data['phone']=$_POST['phone'];

// 	  	  	 $data['email']=$_POST['email'];

 	  	  	 $data['qq']=$_POST['qq'];

 	  	  	 $data['swt']=$_POST['swt'];

 	  	  	 $data['delpass']=$_POST['delpassword'];  

 	  	  	 $data['status']= 0;

 	  	  	 $data['createtime']=mktime();

 	  	  	 $data['company_id']=$_POST['company_id'];

 	  	  	 $data['uid']=$this->uinfo['uid'];
			$data['weixin'] = $_POST['weixin'];
			$data['perform'] = $_POST['perform'];
 	  	  	 $data['cid'] = $_POST['cid'];
 	  	  	 $data['ip'] = get_client_ip();

 	  	  	 if($id = M('Zhaopin')->add($data)){

 	  	  	 	
 	  	  	 	 header("Location: ".U('User/infolist'));
 	  	  	 	
 	  	  	 }else{
 	  	  	 	header("Location: ".U('Index/addinfo'));
 	  	  	 	
 	  	  	 }
 	  	  	 

		}else{

	    		

	    $cid = empty($_POST['cid'])?Exit():$_POST['cid'];

	

	    $province = M('City')->where("status=0 AND province !='' ")->select();
		
		$beijing = M('city')->where('cid=1')->select();
	

	    $citylist =M('City')->where("cid=".$this->cityinfo['cid'])->select();

	    $this->catename = M('category')->where("id=".$cid)->find();

	    $this->company =M('Company')->where("uid=".$this->uinfo['uid'])->find();
	    $this->area = M('Area')->where(array('cityid'=>$this->cityinfo['id']))->select();

	    $this->assign("province",$province);
	    
	    $this->assign("beijing",$beijing);

	    $this->assign("pid",$this->cityinfo['cid']);

	    $this->assign("cityid",$this->cityinfo['id']);

	    $this->assign("citylist",$citylist);

	    
	    $this->assign("cid",$cid);

		
		
	    $this->display();

		}

	}

	/*验证图片*/
	function checkVerify(){

		$verify = $_GET['verify'];

		if(md5($verify) ==$_SESSION['verify'] ){

			 echo 1;

		}else{

			

			echo 0;

		}

	}

	
	/*检查问题*/
	function checkAsk(){

		$index = $_GET['index'];

		

		$s = $_GET['yzask'];

	   $info = $this->asklist[$index];



	   if($info['q']== $s){

	   	   echo(1);

	   }else{

	   	echo(0);

	   }

	}

	function getcity(){
		   $id = $_GET['id'];
		   $list = M('city')->where("cid=".$id)->select();
		    echo(json_encode($list));
		 } 
	  
	   /*
       * 上传图片
       * Enter description here ...
       */
    function upfile(){
     
      	import('ORG.Net.UploadFile');
      	$path=date("Ym").'/';
      	$upload = new UploadFile();// 实例化上传类
      	$upload->maxSize  = 3145728 ;// 设置附件上传大小
      	$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      	$upload->savePath =  './Data/photo/'.$path;// 设置附件上传目录
      	if(!file_exists("./Data/photo/".$path)){
      		mkdir("./Data/photo/".$path);
      	}
      	if(!$upload->upload()) {
      		// 上传错误提示错误信息
			
             return $upload->getErrorMsg();
			 
      	}else{

      		$info = $upload->getUploadFileInfo(); //获取上传结果信息
      		$photopath= str_replace(".","",$info[0]['savepath']);
      		$s=$photopath.$info[0]['savename'];
      		    import('ORG.Util.Image');
			//$Image = new Image();
			// 给avator.jpg 图片添加logo水印
			//$Image->water('.'.$s,'avator.png','',80);
      		return $s;
     

      	}
      	 
      }

    /**
     * 保存64位编码图片
     */

    function saveBase64Image($base64_image_content){
        $image_url = "";
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){

            //图片后缀
            $type = $result[2];

            //保存位置--图片名
            $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
            $image_url = '/Data/photo/'.date('Ym').'/'.$image_name;
            if(!is_dir(dirname('.'.$image_url))){
                mkdir(dirname('.'.$image_url));
                chmod(dirname('.'.$image_url), 0777);
                umask($oldumask);
            }

            //解码
            $decode=base64_decode(str_replace($result[1], '', $base64_image_content));
            if (file_put_contents('.'.$image_url, $decode)){
                $data['code']=0;
                $data['imageName']=$image_name;
                $data['url']=$image_url;
                $data['msg']='保存成功！';
                return $image_url;
            }else{
                $data['code']=1;
                $data['imageName']='';
                $data['url']='';
                $data['msg']='图片保存失败！';
                return "";
            }
        }else{
            $data['code']=1;
            $data['imgageName']='';
            $data['url']='';
            $data['msg']='base64图片格式有误！';
            return "";

        }

    }

	  public function search(){  
        $keyword = $_POST['searchValue'];        
        //$keyword = 'A4';
        $City=M('city');  
		$city=array();
        //这里我做的一个模糊查询到名字或者对应的id,主要目的因为我这个系统是  
        //商城系统里面用到直接看产品ID  
        $map['city|pinyin']  = array('like',$keyword.'%');  
        // 把查询条件传入查询方法  
        if($city = $City->where($map)->select()){  
            $data = json_encode($city);
            //$data = $goods;
            //echo $data;
            $this->ajaxReturn($data); 
           
        }else{  
            $city['content'] = 'failed';
			$data = json_encode($city);
            $this->ajaxReturn($data);  
        }  
    }
     
}
?>
